package gamecontrol;

/**
 * Keeps all the boxes that the current board has, and provide the methods to access and add the boxes. 
 * @author quique
 */
public class CollectionOfBoxes {
        
        /**
         * Keeps all the boxes
         */
	private Box[] boxes;
        
        private int xDimension;
        private int yDimension;
        
        /**
         * Constructor 
         * @param numberOfBoxes
         */
        public CollectionOfBoxes(int xDimension, int yDimension){
            boxes = new Box[xDimension*yDimension];
            this.xDimension = xDimension;
            this.yDimension = yDimension;
        }
	
        /**
         * Position starts in 1
         * @param positionX
         * @param positionY
         * @return the box situated int the given position
         */
	public Box getBox(int positionX, int positionY) {
		return boxes[positionX+(xDimension*(positionY-1))-1];
	}
	
        /**
         * Adds to the current object a new box. 
         * @param box
         */
	public void addBox(Box box) {
            int index = box.getPositionX()+(xDimension*(box.getPositionY()-1))-1;
            boxes[index] = box;
            
	}
        
        /**
         * Swaps the boxes given.
         * @param srcX
         * @param srcY
         * @param desX
         * @param desY
         */
        public void swapBoxes(int srcX, int srcY, int desX, int desY){
            Box aux = new Box();
            aux = this.getBox(desX, desY);
            boxes[desX+(xDimension*(desY-1))-1] = boxes[srcX+(xDimension*(srcY-1))-1];
            boxes[srcX+(xDimension*(srcY-1))-1] = aux;
            //change the box position of hero for the new ones.
            this.getBox(srcX, srcY).setPosition(srcX, srcY);
            //change the box position of hero for the new ones.
            this.getBox(desX, desY).setPosition(desX, desY);
            
        }
	 
}
 
